package com.bignerdranch.android.computer;

import android.app.Activity;
import android.os.Bundle;
//import androidx.appcompat.app.AppCompatActivity;
//import android.os.Bundle;
import java.text.NumberFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {

    double firstNum=0;//第一个输入的数据
    char currentSign='+';//记录第一次输入的符号
    StringBuffer currentNum=new StringBuffer();//得到textview中的数据
    boolean isFirstPoint=false;//第一个数据是否是小数点
    TextView txtResult;//输出结果

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtResult=(TextView) findViewById(R.id.txtResult);
    }

    //1.输入框恢复与初始化
    public void init(){
        currentNum.delete(0,currentNum.length());//设置当前textview中的值为0
        isFirstPoint=false;
    }

    //2.数制转换函数
    public double stringToDouble(){
        if(currentNum.length()==0){
            return 0;
        }
        double result=Double.parseDouble(currentNum.toString());
        return result;
    }

    //3.计算函数
    public double calcu(){
        double result=0;
        switch (currentSign){
            case '+':
                result=firstNum+stringToDouble();
                break;
            case '-':
                result=firstNum-stringToDouble();
                break;
            case '*':
                result=firstNum*stringToDouble();
                break;
            case '/':
                result=firstNum/stringToDouble();
                break;
            default:
                break;
        }
        //对小数点后的数据进行格式化
        NumberFormat format=NumberFormat.getInstance();
        format.setMaximumFractionDigits(2);
        result=Double.parseDouble(format.format(result));
        return result;
    }

    //4显示数据函数
    public void display(){
        txtResult.setText(currentNum.toString());
    }

    //5数字按钮单击响应函数
    public void digital_click(View view){
        Button btnDigital=(Button)view;
        char text= btnDigital.getText().charAt(0);
        currentNum.append(text);
        display();
    }

    //6.加法按钮单击
    public void add(View view){
        double result=calcu();
        txtResult.setText(String.valueOf(result));
        firstNum=result;
        currentSign='+';
        init();
    }
    //7.单击减法
    public void sub(View view) {
        double result = calcu();
        txtResult.setText(String.valueOf(result));
        firstNum = result;
        currentSign = '-';
        init();
    }
    //8.单击乘法
    public void mul(View view) {
        double result = calcu();
        txtResult.setText(String.valueOf(result));
        firstNum = result;
        currentSign = '*';
        init();
    }
    //9.单击除法
    public void div(View view) {
        double result = calcu();
        txtResult.setText(String.valueOf(result));
        firstNum = result;
        currentSign = '/';
        init();
    }
    //10.处理等于函数
    public void equ(View view){
        double result=calcu();
        txtResult.setText(String.valueOf(result));
        firstNum=result;
        currentSign='+';
        init();

    }
    //11.小数点按钮单击相应函数
    public void point_click(View view){
        if(isFirstPoint){
            return;//当第一个数据为小数点时，程序返回
        }
        if(currentNum.length()==0){
            return;//当没有输入的数据时返回
        }
        Button btnPoint=(Button) view;
        char text=btnPoint.getText().charAt(0);
        currentNum.append(text);
        isFirstPoint=true;
        display();
    }

    //12.删除按钮单击相应
    public void del(View view){
        if(currentNum.length()>=1){
            currentNum.delete(currentNum.length()-1,currentNum.length());
        }
        if(currentNum.length()==0){
            init();
            display();
        }
        txtResult.setText(currentNum);

    }
}

